package gui;

import java.awt.Button;
import java.awt.Container;
import java.awt.Label;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.IOException;
import java.util.ArrayList;

import javax.swing.BoxLayout;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import core.DmuList;

public class MenuFrame extends JFrame implements ActionListener{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = -7660691149449025416L;
	//gui
	private Label fileStatus= new Label("No inputfile selected");
	private Button newChartWindow;
	JFileChooser chooser = new JFileChooser();
	Button fileSelection;
	
	//logic
	public static ArrayList<PdmFrame> pdmFrames = new ArrayList<PdmFrame>();
	private DmuList dmuList;
	private double[] standardUrmWeights = new double[]{21, 34, 11, 13, 15, 22, 32 ,21};
	
	
	public MenuFrame() {
		initialize();
		this.pack();
		this.setVisible(true);
		
		runDemo();
	}
	private void initialize(){
		this.setTitle("PDM Prototype Menu");
		this.addWindowListener (
			      new WindowAdapter() {
			        public void windowClosing (WindowEvent e) {
			          destroy();
			    } } );
		
		JPanel menu = new JPanel();
		menu.setLayout(new BoxLayout(menu, BoxLayout.Y_AXIS));
		fileSelection = new Button("Select Inputfile");
		fileSelection.setActionCommand("FILESELECTION");
		fileSelection.addActionListener(this);
		menu.add(fileSelection);
		menu.add(fileStatus);
		newChartWindow = new Button("New chart window");
		newChartWindow.setActionCommand("CHARTWINDOW");
		newChartWindow.addActionListener(this);
		newChartWindow.setEnabled(false);
		menu.add(newChartWindow);
		this.setContentPane(menu);
	}
	private void addNewWindow(){
		if(this.dmuList != null){
			PdmFrame newWindow = new PdmFrame(this.dmuList, standardUrmWeights);
			newWindow.initialize();
			MenuFrame.pdmFrames.add(newWindow);
		}
	}
	private void runDemo(){
		fileStatus.setText("Demodata");
		fileSelection.setEnabled(false);
		try {
			this.dmuList = util.Util.LoadFile("PDM1.csv");
		} catch (NumberFormatException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		this.newChartWindow.setEnabled(true);
	}
	private void selectFile(){
		String filename = getSelectedFile();
		
		if(filename != null)
		try{
			this.dmuList = util.Util.LoadFile(filename);
			this.newChartWindow.setEnabled(true);
			this.fileStatus.setText(filename);
		}
		catch(Exception e){
			JOptionPane.showMessageDialog(null, "Wrong input format", "Error", JOptionPane.OK_OPTION);
			e.printStackTrace();
			return;
		}
	}
	private String getSelectedFile(){
		int returnVal = chooser.showOpenDialog(this);
	    if(returnVal == JFileChooser.APPROVE_OPTION) {
	       System.out.println("You chose to open this file: " +
	            chooser.getSelectedFile().getAbsolutePath());
	       return chooser.getSelectedFile().getAbsolutePath();
	    }
	    return null;
	}
	private void destroy(){
			for(PdmFrame f:this.pdmFrames){
				if(f != null){
					f.dispose();
				}
			}
			this.dispose();
			System.exit(0);
	}
	@Override
	public void actionPerformed(ActionEvent arg0) {
		if(arg0.getActionCommand() == "FILESELECTION"){
			this.selectFile();
		}
		else if(arg0.getActionCommand() == "CHARTWINDOW"){
			this.addNewWindow();
		}
		
	}

}
